﻿#include<stdio.h>
#include<stdlib.h>
#include<string.h>



char* S_getfileall(char* fname){
	FILE* fp;
	char* str;
	char txt[10000];
	int filesize;
	int total = 0;
	if ((fp = fopen(fname, "r", "utf-8")) == NULL){
		printf("打开文件%s错误\n", fname);
		return NULL;
	}
	fseek(fp, 0, SEEK_END);
	filesize = ftell(fp);
	str = (char*)malloc(filesize);
	str[0] = 0;
	rewind(fp);
	while ((fgets(txt, 10000, fp)) != NULL){
		strcat(str, txt);
		int len = strlen(txt);
		total += len;
		printf("%d...%d....%d......%s", filesize, total, len, txt);
	}
	fclose(fp);
	return str;
}


//函数返回fname指定文件的全部内容，如果打不开文件，则返回NULL，并显示打开文件错误
char* getfileall(char* fname){
	FILE* fp;
	char* str;
	char txt[10000];
	int filesize; 
	if ((fp = fopen(fname, "r","utf-8")) == NULL){
		printf("打开文件%s错误\n", fname);
		return NULL;
	}
	fseek(fp, 0, SEEK_END);
	filesize = ftell(fp);
	str = (char*)malloc(filesize);
	str[0] = 0;
	rewind(fp);
	while ((fgets(txt, 10000, fp)) != NULL){
		strcat(str, txt);  
	}
	fclose(fp);
	return str;
}
int S_main22(){
	char* p;
	char* fname = "D:/gitHub/cpp/cslua_5.4/prj/cslua/code.cs";
	p = getfileall(fname);
	printf("字符串长度........%d", strlen(p));
	if (p != NULL) puts(p);
}